package com.hzjh.h5mall.ui.register

import android.graphics.Paint
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.R
import com.hzjh.base.ext.data.Configs
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.router.ext.navigationTo
import com.hzjh.base.style.weight.ToastKt.toastCenter
import com.hzjh.base.utils.function.countDown
import com.hzjh.base.utils.function.isEmail
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.base.utils.other.KeyboardUtils
import com.hzjh.h5mall.BuildConfig
import com.hzjh.h5mall.databinding.ActivityRegisterBinding
import com.hzjh.h5mall.model.register.RegisterVM

@Route(path = RouteUrl.REGISTER_ACTIVITY)
class RegisterActivity : JHBaseActivity<ActivityRegisterBinding,RegisterVM>() {
    private var ifCanSendMsg=true
    private var isAgree = false
    override fun initData() {

    }

    override fun initView() {
        viewBinding.btnRegister.apply {
            paint.flags = Paint.UNDERLINE_TEXT_FLAG; //下划线
            paint.isAntiAlias = true;//抗锯齿
        }
        viewBinding.inputEmail.setTextChangeListener {
            changeRegisterStatus()
        }
        viewBinding.inputCode.addTextChangedListener {
            changeRegisterStatus()
        }
        viewBinding.inputPassword.setTextChangeListener {
            changeRegisterStatus()
        }
        viewBinding.inputEnterAgain.setTextChangeListener {
            changeRegisterStatus()
        }

        viewBinding.ivCheckAgree.setOnClickListener {
            changeAgreeStatus()
        }
        viewBinding.tvCheckAgree.setOnClickListener {
            changeAgreeStatus()
        }

        viewBinding.btnSend.setOnClickListener {
            //发送验证码
            val email = viewBinding.inputEmail.text.toString()
            if(!email.isEmail()){
                toastCenter(CommUtils.getString(R.string.jh_email_check_toast))
                return@setOnClickListener
            }
            if(ifCanSendMsg){
                viewModel.reqEmailCaptcha(email)
                //发送短信验证码
                countDown(59,
                    start = {
                        ifCanSendMsg=false
                        viewBinding.btnSend.isEnabled=false
                        viewBinding.btnSend.text = CommUtils.getString(R.string.jh_send_time, 60)
                    },
                    end = {
                        ifCanSendMsg=true
                        viewBinding.btnSend.isEnabled=true
                        viewBinding.btnSend.text = CommUtils.getString(R.string.jh_send)
                    },
                    next = {
                        viewBinding.btnSend.text = CommUtils.getString(R.string.jh_send_time, it)
                    }
                )
            }
        }

        viewBinding.btnRegister.setOnClickListener{
            //注册
            KeyboardUtils.hideSoftkeyboard(this)
            if(!isAgree){
                toastCenter(CommUtils.getString(R.string.jh_agree_policy_toast))
                return@setOnClickListener
            }
            val email = viewBinding.inputEmail.text.toString()
            if(!email.isEmail()){
                toastCenter(CommUtils.getString(R.string.jh_email_check_toast))
                return@setOnClickListener
            }
            val captcha = viewBinding.inputCode.text.toString()
            val password = viewBinding.inputPassword.text.toString()
            val enterAgain = viewBinding.inputEnterAgain.text.toString()
            if(password != enterAgain){
                toastCenter(CommUtils.getString(R.string.jh_password_check_toast))
                return@setOnClickListener
            }
            viewModel.reqEmailRegister(email,captcha,password)
        }

        viewBinding.tvUserAgreement.setOnClickListener {
            //用户协议
            navigationTo(RouteUrl.WEB_ACTIVITY){
                withString(RouteUrl.PARAMS_WEB_URL, BuildConfig.h5Url+ Configs.USER_AGREEMENT_URL)
                withBoolean(RouteUrl.PARAMS_WEB_BAR_VISIBILITY,true)
                withBoolean(RouteUrl.PARAMS_WEB_TITLE_BAR_VISIBILITY,true)
            }
        }
    }

    private fun changeRegisterStatus(){
        viewBinding.btnRegister.isEnabled=!viewBinding.inputEmail.text.isNullOrBlank()
                &&!viewBinding.inputPassword.text.isNullOrBlank()
                &&!viewBinding.inputEnterAgain.text.isNullOrBlank()
    }
    private fun changeAgreeStatus(){
        isAgree = !isAgree
        if (isAgree) {
            viewBinding.ivCheckAgree.setImageResource(R.drawable.ic_checkbox_true)
        } else {
            viewBinding.ivCheckAgree.setImageResource(R.drawable.ic_checkbox_false)
        }
    }

    override fun initObservable() {
        super.initObservable()
    }

    override fun initViewBinding(): ActivityRegisterBinding {
        return ActivityRegisterBinding.inflate(layoutInflater)
    }
}